namespace YahooAuth
{
   using System;
   using System.Data;
   using System.Data.SqlClient;
   
   
   public class SP
   {
      
      public static int YahooToken_Delete(System.Data.SqlClient.SqlConnection connection, System.Data.DataTable table, YahooAuth.YahooToken Token)
      {
         System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
         int result = 0;
         cmd.Connection = connection;
         cmd.CommandText = "YahooToken_Delete";
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Token.ID;
         connection.Open();
         try
         {
            if ((table != null))
            {
               System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
               if ((table.Columns.Count == 0))
               {
                  table.TableName = "YahooToken_Delete";
                  for (int i = 0; (i < reader.FieldCount); i = i + 1
                  )
                  {
                     System.Type type = reader.GetFieldType(i);
                     string name = reader.GetName(i);
                     table.Columns.Add(name, type);
                  }
               }
               table.Clear();
               for (
               ; reader.Read(); result = result + 1
               )
               {
                  System.Data.DataRow row = table.NewRow();
                  object[] rowdata = new object[reader.FieldCount];
                  reader.GetValues(rowdata);
                  row.ItemArray = rowdata;
                  table.Rows.Add(row);
               }
               reader.Close();
            }
            else
            {
               result = cmd.ExecuteNonQuery();
            }
         }
         catch (SqlException ex)
         {
             result = -1;
         }
         finally
         {
            connection.Close();
         }
         return result;
      }
      
      public static int YahooToken_FindAll(System.Data.SqlClient.SqlConnection connection, System.Data.DataTable table)
      {
         System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
         int result = 0;
         cmd.Connection = connection;
         cmd.CommandText = "YahooToken_FindAll";
         cmd.CommandType = CommandType.StoredProcedure;
         connection.Open();
         try
         {
            if ((table != null))
            {
               System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
               if ((table.Columns.Count == 0))
               {
                  table.TableName = "YahooToken_FindAll";
                  for (int i = 0; (i < reader.FieldCount); i = i + 1
                  )
                  {
                     System.Type type = reader.GetFieldType(i);
                     string name = reader.GetName(i);
                     table.Columns.Add(name, type);
                  }
               }
               table.Clear();
               for (
               ; reader.Read(); result = result + 1
               )
               {
                  System.Data.DataRow row = table.NewRow();
                  object[] rowdata = new object[reader.FieldCount];
                  reader.GetValues(rowdata);
                  row.ItemArray = rowdata;
                  table.Rows.Add(row);
               }
               reader.Close();
            }
            else
            {
               result = cmd.ExecuteNonQuery();
            }
         }
         catch (SqlException ex)
         {
            throw ex;
         }
         finally
         {
            connection.Close();
         }
         return result;
      }

       public static YahooToken YahooToken_FindSingleByFrob(System.Data.SqlClient.SqlConnection connection, string Frob)
      {
          YahooToken Token = new YahooToken();
         System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
         int result = 0;
         cmd.Connection = connection;
         cmd.CommandText = "YahooToken_FindSingle";
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.Add("@Frob", SqlDbType.Text).Value = Frob;
         connection.Open();
         try
         {
             DataTable table = new DataTable();
            if ((table != null))
            {
               System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
               if ((table.Columns.Count == 0))
               {
                  table.TableName = "YahooToken_FindSingle";
                  for (int i = 0; (i < reader.FieldCount); i = i + 1
                  )
                  {
                     System.Type type = reader.GetFieldType(i);
                     string name = reader.GetName(i);
                     table.Columns.Add(name, type);
                  }
               }
               table.Clear();
               for ( ; reader.Read(); result = result + 1)
               {
                  System.Data.DataRow row = table.NewRow();
                  object[] rowdata = new object[reader.FieldCount];
                  reader.GetValues(rowdata);
                  row.ItemArray = rowdata;
                  table.Rows.Add(row);
               }
               reader.Close();

              //Map Table to YahooToken Object
               if (table.Rows.Count > 0)
               {
                   for (int i = 0; i < table.Columns.Count; i++)
                   {
                       System.Reflection.PropertyInfo p = Token.GetType().GetProperty(table.Columns[i].ColumnName);
                       Object v = Convert.ChangeType(table.Rows[0][i], p.PropertyType);
                       p.SetValue(Token, v, null);

                   }
               }
            }
         }
         catch (SqlException ex)
         {
            //throw ex;
             return null;
         }
         finally
         {
            connection.Close();
         }
         return Token;
      }
      
      public static int YahooToken_Insert(System.Data.SqlClient.SqlConnection connection, System.Data.DataTable table, ref YahooToken Token)
      {
         System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
         int result = 0;
         cmd.Connection = connection;
         cmd.CommandText = "YahooToken_Insert";
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Token.ID;
         cmd.Parameters.Add("@Frob", SqlDbType.VarChar).Value = Token.Frob;
         cmd.Parameters.Add("@Token", SqlDbType.VarChar).Value = Token.Token;
         cmd.Parameters.Add("@AddingDate", SqlDbType.DateTime).Value = Token.AddingDate;
         connection.Open();
         try
         {
            if ((table != null))
            {
               System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
               if ((table.Columns.Count == 0))
               {
                  table.TableName = "YahooToken_Insert";
                  for (int i = 0; (i < reader.FieldCount); i = i + 1
                  )
                  {
                     System.Type type = reader.GetFieldType(i);
                     string name = reader.GetName(i);
                     table.Columns.Add(name, type);
                  }
               }
               table.Clear();
               for (
               ; reader.Read(); result = result + 1
               )
               {
                  System.Data.DataRow row = table.NewRow();
                  object[] rowdata = new object[reader.FieldCount];
                  reader.GetValues(rowdata);
                  row.ItemArray = rowdata;
                  table.Rows.Add(row);
               }
               reader.Close();
            }
            else
            {
               result = cmd.ExecuteNonQuery();
            }
         }
         catch (SqlException ex)
         {
            //throw ex;
             result = -1;
         }
         finally
         {
            connection.Close();
         }
         return result;
      }
      
      public static int YahooToken_UpdateToken(System.Data.SqlClient.SqlConnection connection, System.Data.DataTable table, YahooToken Token)
      {
         System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
         int result = 0;
         cmd.Connection = connection;
         cmd.CommandText = "YahooToken_Update";
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.Add("@Frob", SqlDbType.VarChar).Value = Token.Frob;
         cmd.Parameters.Add("@Token", SqlDbType.VarChar).Value = Token.Token;

         connection.Open();
         try
         {
            if ((table != null))
            {
               System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
               if ((table.Columns.Count == 0))
               {
                  table.TableName = "YahooToken_Update";
                  for (int i = 0; (i < reader.FieldCount); i = i + 1
                  )
                  {
                     System.Type type = reader.GetFieldType(i);
                     string name = reader.GetName(i);
                     table.Columns.Add(name, type);
                  }
               }
               table.Clear();
               for (
               ; reader.Read(); result = result + 1
               )
               {
                  System.Data.DataRow row = table.NewRow();
                  object[] rowdata = new object[reader.FieldCount];
                  reader.GetValues(rowdata);
                  row.ItemArray = rowdata;
                  table.Rows.Add(row);
               }
               reader.Close();
            }
            else
            {
               result = cmd.ExecuteNonQuery();
            }
         }
         catch (SqlException ex)
         {
             result = -1;
         }
         finally
         {
            connection.Close();
         }
         return result;
      }
   }
   //*******************************************************
    static public class DBConnections
    {
        public static System.Data.SqlClient.SqlConnection CreateConnection(string DBConnectionString)
        {
            return new System.Data.SqlClient.SqlConnection(DBConnectionString);
        }
    }//End Class
}//End Namespace
